/** Exhaustive NN Search
 *  
 * 
 * @author Yong Boon, Lim (yongboon.lim@gmail.com)
 */

package core;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import util.DocUtils;
import util.FileFinder;
import util.HammingDist;

public class NN extends Core {
	
	public NN(String trainDataDir) {
		super(trainDataDir);
		_nnDescShort = "Naive-NN";
		_nnDescLong = "Naive Nearest Neighbor";
	}
	
	public void init() {
		// Nothing to initialize
	}
	
	@Override
	public void build() {
		// No index to build;
		
	}
	
	// Retrieve the closest doc
	public String query(String targetDataName) {
		File targetDataFile = new File(targetDataName);
		Map<Object, Double> targetWordCount = DocUtils.ConvertToFeatureMapFilter(DocUtils.ReadFile(targetDataFile));
		ArrayList<File> files = FileFinder.GetAllFiles(_trainDataDir, "", true);
		
		int minDist = Integer.MAX_VALUE;
		int dist;
		String fileName = null;

		// Loop through all the training files and find the closest to target file
		for (File f : files) {
			Map<Object, Double> trainDataWordCount = DocUtils.ConvertToFeatureMapFilter(DocUtils.ReadFile(f));
			dist = HammingDist.calc(targetWordCount.keySet(), trainDataWordCount.keySet());
			
			if (dist < minDist) {
				minDist = dist;
				fileName = f.getPath();
			}			
		}
		
		return fileName;
	}
	
	public int size() {
		return 0;
	}
	
	public void clear() {
		// Nothing to clear
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NN nn = new NN("data/test_data");
		
		System.out.println(nn.query("data/test_data/00002.txt"));

	}



}
